#include <atomic>
#include "consensus.h"

using std::atomic;

template <class T>
class CasConsensus : public Consensus<T> {
	public :

	CasConsensus(size_t N):
		Consensus<T>(N) {
		sync_ = INVALID_VALUE;
//		old_ = INVALID_VALUE;
	}
	
	virtual T Decide() {
		int i = omp_get_thread_num();
		int inval = INVALID_VALUE;//old_.load();
		assert(i < this->N_);
		sync_.compare_exchange_strong(inval, i);
//		old_.store(sync_.load());
			
		return this->proposed_[sync_.load()];
	}
	
	private :

	static const int INVALID_VALUE = -1;	

	atomic<int> sync_;
//	atomic<int> old_;
	
};
